layui.use([ 'form', 'layer','jquery','IconFonts' ], function() {
	var form = layui.form, 
		layer = layui.layer,
		IconFonts = layui.IconFonts,
		$ = layui.jquery ;
	var id = $('#id').val();
	form.render();
	//上级菜单
	$.post(layui.cache['contentPath']+"/sys-menu/findList", {menuTypeNotIn:2}, function(data) {
		data = eval('('+data+')');
		if(data.code=="0"){
			var selectMenu = xmSelect.render({
			    el: '#xm-select-menuTrees',
			    initValue:[$('#parentId').val()],
			    prop:{name:'name',value:'id'},
			    name:"parentId",
			    data:data.data,
			    clickClose:true,
			    radio:true,
			    paging:true,
			    pageSize:5,
			    template({ item, sels, name, value }){
					return item.name + '<span class="layui-word-aux" style="position: absolute; right: 10px;">'+item.parentName+'</span>';
				},
			    on:function(data){
			    	var arr = data.arr ;
			    	if(data.isAdd){
			    		$('#parentId').val(arr[0].id);
			    	}else{
			    		$('#parentId').val("");
			    	}
			    }
			});
		}
	});
	//菜单类型-下拉
	$.post(layui.cache['contentPath']+"/sys-dict/findItemList", {dictCode:'CORE-MENU'}, function(data) {
		data = eval('('+data+')');
		if(data.code=="0"){
			xmSelect.render({
			    el: '#xm-select-menuTypes',
			    initValue:[$('#menuType').val()],
			    prop:{name:'name',value:'code'},
			    name:"menuType",
			    data:data.data,
			    clickClose:true,
			    radio:true,
			    paging:true,
			    pageSize:10,
			    on:function(data){
			    	var arr = data.arr ;
					rebulidActionUrl(arr[0]);
					console.log("arr[0]=",arr[0]);
					if(data.isAdd){
			    		$('#menuType').val(arr[0].id);
			    	}else{
			    		$('#menuType').val("");
			    	}
			    }
			});
		}
	});
	//图标下拉列表
	$.post(layui.cache['contentPath']+"/icons/findList", {menuTypeNotIn:2}, function(data) {
		data = eval('('+data+')');
		if(data.code=="0"){
			var iconSelect = xmSelect.render({
				el: '#xm-select-icons',
				initValue:[$('#icon').val()],
				prop:{name:'className',value:'className'},
				filterable:true,
				model: {
					label: {
						block: {
							template: function(item, sels){
								return '<i class="'+item.className+'" aria-hidden="true"></i>' + item.item.className;
							},
						},
					}
				},
				template({ item, sels, name, value }){
					var icons = new Array();
					if(item.iconGroup=='layui-icon')
						icons.push('<i class="'+item.code+' '+item.className+'"></i>');
					else
						icons.push('<i class="'+item.className+'" aria-hidden="true"></i>');
					icons.push('<span class="layui-word-aux" style="position: absolute; right: 10px;">'+item.className+'</span>');
					return  icons.join("");
				},
				name:"icon",
				data:data.data,
				clickClose:true,
				radio:true,
				paging:true,
				pageSize:10
			});
		}
	});
	// 监听提交
	form.on('submit(save)', function(data) {
		var submitUrl = layui.cache['contentPath']+'/sys-menu/save';
		if(id!=undefined && id!=""){
			submitUrl = layui.cache['contentPath']+'/sys-menu/modify';
		}
		$.ajax({
			type:'POST',
			url:submitUrl,
			data:JSON.stringify(data.field),
			contentType: "application/json",
			success:function(rs){
				rs = eval('('+rs+')');
				if(rs.code==0){
					layer.msg("操作成功",{icon:1,time:3000});
					// 关闭当前frame
					xadmin.close();
					// 可以对父窗口进行刷新
					xadmin.father_reload();
				}else{
					layer.msg(rs.msg,{icon:2,time:3000});
				}
			},
			error:function(status,errors){
				layer.alert('请求错误，错误码：'+JSON.stringify(status));
			}
		});
		return false;
	});
});
/**
 * 根据菜单类型（按钮、菜单、目录），控制URL/图标是否显示
 * @param data
 * @returns
 */
function rebulidActionUrl(data){
	$("#F_actionUrl").show();
	$("#F_icon").show();
	if(data.code!="1"){
		$("#F_actionUrl").hide();
		$("#actionUrl").val("");
	}
	if (data.code=="2" || data.code=="3"){
		$("#F_icon").hide();
		$("#icon").val("");
	}
}